<?

class Main {

    public $DB, $site;

    function __construct() {
        $this->DB = Database::Init();
        $this->site = $this->GetSiteSettings();
        $this->site['template'] = $this->GetCurrentTemplate()['template'];
        unset($this->site['template_map']);
        $this->site['template_uri'] = "/kernel/templates/" . $this->site['template'];
        $this->site['template_path'] = TEMPLATES . "/" . $this->site['template'];
        $this->site['controllers'] = TEMPLATES . "/" . $this->site['template'] . "/controllers";
    }

    function GetSiteSettings($name = false) {
        $settingsObj = $this->DB->select()
                ->table('sites', "s")
                ->table('languages', "l")
                ->field(array('s.*', "l.code" => "lang"));
        if (!$name) {
            $settingsObj->cond('is_default', 1);
        } else {
            $settingsObj->cond('code', $name);
        }
        $settingsObj->cond('l.id', 's.lang')->noquotes();
        $result = $settingsObj->execute();
        return $result->Fetch();
    }

    function GetCurrentTemplate() {
        $map = unserialize($this->site['template_map']);
        $current_template = false;
        foreach ($map as $template) {
            $path = parse_url($_SERVER['REQUEST_URI']);
            if ($template['type'] == "query" && preg_match("/" . $template['condition'] . "/", $path['query'])) {
                $current_template = $template;
            } elseif ($template['type'] == "path" && $path['path'] == $template['condition']) {
                $current_template = $template;
            } elseif ($template['type'] == "site" && $this->site['code'] == $template['condition']) {
                $current_template = $template;
            }
        }
        return $current_template;
    }

    function IncludeLangFile($file) {
        $filename = basename($file);
        $dirname = dirname($file);
        $langfile = $dirname . "/lang/" . $this->site['lang'] . "/" . $filename;
        if (file_exists($langfile)) {
            include_once $langfile;
            if(isset($MESS)){
                $GLOBALS['MESS'] = array_merge($GLOBALS['MESS'], $MESS);
            }
        }
    }

    function GetTitle() {
        return Buffer::Get('TITLE');
    }

    function SetTitle($title) {
        Buffer::Set('TITLE', $title);
    }

    function Controller($ns_name, $template = false, $params = array()) {
        $MAIN = &$this;
        $template = $template ? $template : "default";
        list($ns, $controller) = explode(".", $ns_name, 2);
        $filename = CONTROLLERS."/$ns/$controller/controller.php";
        
        $view = CONTROLLERS."/$ns/$controller/views/$template/view.php";
        if(file_exists($filename)){
            $result = array();
            include $filename;
            if(file_exists($view)){
                include $view;
            }else{
                Logger::error("View of $ns_name:$template not found");
            }
        }else{
            Logger::error("Controller $ns_name not found");
        }
    }
    
    function AddHeadStyle($path, $type = "css"){
        $str = "<link type=\"text/css\" rel=\"stylesheet".($type = "less" ? "/less" : false)."\" href=\"$path\">";
        Buffer::Add('HEAD_STYLES', $str);
    }
    
    function AddHeadScript($path){
        $str = "<script type=\"text/javascript\" src=\"$path\"></script>";
        Buffer::Add('HEAD_STYLES', $str);
    }
    
    function GetHeadStyles(){
        print Buffer::get('HEAD_STYLES');
    }
    function GetHeadScripts(){
        print Buffer::get('HEAD_SCRIPTS');
    }
}
